package br.iars.web.model.impl;

import java.util.List;

import javax.persistence.Query;

import br.iars.web.model.api.UserModel;
import br.iars.web.model.entity.User;

public class UserModelImpl extends GenericModelImpl<User, Long> implements UserModel {

	private static final long serialVersionUID = -4999789584254119497L;

	@SuppressWarnings("unchecked")
	public User findByEmail(String email) {
		User user = null;
		try {
			Query query = getEntityManager().createQuery("FROM User u WHERE u.email = :email");
			query.setParameter("email", email);
			List<User> users = (List<User>) query.getResultList();

			if (users != null && !users.isEmpty()) {
				user = users.get(0);
			}
		} catch (RuntimeException e) {
			getLogger().error("Falha ao buscar entidade: " + getPersistentClassName() + ", com o e-mail: " + email, e);
			throw e;
		}
		return user;
	}
}
